package com.egao.common.test.controller;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.egao.common.article.entity.TArticle;
import com.egao.common.article.entity.TArticleType;
import com.egao.common.article.entity.dto.ArticleDto;
import com.egao.common.article.service.ITArticleService;
import com.egao.common.article.service.ITArticleTypeService;
import com.egao.common.core.shiro.ShiroUtil;
import com.egao.common.core.utils.UrlTranslate;
import com.egao.common.core.utils.UserUtil;
import com.egao.common.remark.entity.TArticleRemark;
import com.egao.common.remark.entity.TCourseRemark;
import com.egao.common.remark.entity.TLikes;
import com.egao.common.remark.entity.TReleaseRemark;
import com.egao.common.remark.entity.dto.CourseRemarkDto;
import com.egao.common.remark.entity.vo.TCourseRemarkVo;
import com.egao.common.remark.mapper.TCourseRemarkMapper;
import com.egao.common.remark.mapper.TLikesMapper;
import com.egao.common.remark.service.ITArticleRemarkService;
import com.egao.common.remark.service.ITCourseRemarkService;
import com.egao.common.remark.service.ITLikesService;
import com.egao.common.remark.service.ITReleaseRemarkService;
import com.egao.common.system.entity.User;
import com.egao.common.system.mapper.UserTwoMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.egao.common.core.shiro.CustomizedToken;
import com.egao.common.core.utils.FileUploadUtil;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.system.entity.UserTwo;
import com.egao.common.system.service.UserTwoService;
import com.egao.common.test.entity.Classify;
import com.egao.common.test.entity.Course;
import com.egao.common.test.entity.Order;
import com.egao.common.test.entity.Present;
import com.egao.common.test.entity.Release;
import com.egao.common.test.entity.Teacher;
import com.egao.common.test.entity.Video;
import com.egao.common.test.service.ClassifyService;
import com.egao.common.test.service.CourseService;
import com.egao.common.test.service.LunboService;
import com.egao.common.test.service.MenurService;
import com.egao.common.test.service.OrderService;
import com.egao.common.test.service.PresentService;
import com.egao.common.test.service.ReleaseService;
import com.egao.common.test.service.TeacherService;
import com.egao.common.test.service.VideoService;
import com.wf.captcha.utils.CaptchaUtil;

@Controller
@RequestMapping("/default")
@Slf4j
public class DefaultController {
	
	@Autowired
	private MenurService menurService;
	@Autowired
	private LunboService lunboService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private ClassifyService classifyService;
	@Autowired
	private TeacherService teacherService;
	@Autowired
	private VideoService videoService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private PresentService presentService;
	@Autowired
	private ReleaseService releaseService;
	@Autowired
	private UserTwoService usertwoService;
	@Autowired
	private ITArticleService itArticleService;
	@Autowired
	private ITArticleTypeService itArticleTypeService;
	@Autowired
	private ITCourseRemarkService itCourseRemarkService;
	@Autowired
	private ITReleaseRemarkService itReleaseRemarkService;
	@Autowired
	private ITArticleRemarkService itArticleRemarkService;
	@Autowired
	private ITLikesService itLikesService;
	@Autowired
	private TLikesMapper tLikesMapper;


	@RequestMapping("/default")
    public String index(Model model) {
		Subject subject = SecurityUtils.getSubject();
        Object object = subject.getPrincipal();
		if(object != null) {
			try {
				UserTwo userTwo = (UserTwo)object;
				model.addAttribute("user", (UserTwo)object);
			}catch (ClassCastException  e){
				User user = (User)object;
				subject.logout();
				//shiroUtil.singleUseLogin(user.getUsername());
				UserTwo user2 = new UserTwo();
				user2.setUsername("游客");
				user2.setUserId(-1);
				model.addAttribute("user", user2);
			}
		}
		List<Course> courses = courseService.listGgkc();
		for (Course course :courses){
			log.info("课程图片地址: {}",course.getImageUrl());
		}
		List<Course> courses1 = courseService.listZykc();
		model.addAttribute("menu", menurService.list());
		model.addAttribute("lunbo", lunboService.list());
		model.addAttribute("ggkc", courseService.listGgkc());
		model.addAttribute("zykc", courseService.listZykc());
		model.addAttribute("mfkc", courseService.listMfkc());
        return "default/default.html";
    }
	
	@RequestMapping("/course")
    public String course(Model model){
		Subject subject = SecurityUtils.getSubject();
        Object object = subject.getPrincipal();

		if(object != null) {
        	model.addAttribute("user", (UserTwo)object);
        }
		model.addAttribute("menu", menurService.list());
		model.addAttribute("classify", classifyService.list());
        return "default/course.html";
    }
	
	@RequestMapping("/present")
    public String present(Model model) {
		Subject subject = SecurityUtils.getSubject();
        Object object = subject.getPrincipal();
        if(object != null) {
        	model.addAttribute("user", (UserTwo)object);
        }
		model.addAttribute("menu", menurService.list());
		model.addAttribute("present", presentService.list());
		model.addAttribute("lunbo", lunboService.list());
        return "default/present.html";
    }
	
    @ResponseBody
    @PostMapping("/upload")
    public JsonResult upload(@RequestParam MultipartFile file) {
        return FileUploadUtil.upload(file);
    }
    
    @ResponseBody
    @PostMapping("/edit_upload")
    public JsonResult edit_upload(@RequestParam MultipartFile file) {
        return FileUploadUtil.edit_upload(file);
    }
	
	@RequestMapping("/detail/{id}/")
	public String detail(@PathVariable("id") Integer id,Model model) {
		Subject subject = SecurityUtils.getSubject();
        Object object = subject.getPrincipal();
        if(object != null) {
				Order order = orderService.getOne(new QueryWrapper<Order>().eq("course_id", id).eq("user_id", ((UserTwo)object).getUserId()));
				if(order != null) {
					model.addAttribute("order",order);
				}else {
					model.addAttribute("order",null);
				}
				model.addAttribute("user", (UserTwo)object);
        }else {
        	model.addAttribute("order",null);
        }
		Course course = courseService.getById(id);
		Teacher teacher = teacherService.getById(course.getTeacherId());
		Classify classify = classifyService.getById(course.getClassifyId());

		List<TCourseRemark> remarkList = itCourseRemarkService.getAllCourseRemark(id);
		List<TCourseRemark> remarkLikes = getCourseRemarkLikes(remarkList);

		model.addAttribute("course", course);
		model.addAttribute("teacher", teacher);
		model.addAttribute("classify", classify);
		model.addAttribute("videos", videoService.list(new QueryWrapper<Video>().eq("course_id", course.getId())));
		model.addAttribute("menu", menurService.list());
		model.addAttribute("tuijian", courseService.listXie());
		model.addAttribute("remark", remarkLikes);
		return "default/detail.html";
	}

	
	@RequestMapping("/detail2/{id}/")
	public String detail2(@PathVariable("id") Integer id,Model model) {
		Subject subject = SecurityUtils.getSubject();
        Object object = subject.getPrincipal();
        if(object != null) {
        	model.addAttribute("user", (UserTwo)object);
        }
		Release release = releaseService.getById(id);
		UserTwo user = usertwoService.getById(release.getUserId());

		List<TReleaseRemark> remarkList =  itReleaseRemarkService.getAllReleaseRemark(id);
		List<TReleaseRemark> releaseRemarkLikes = getReleaseRemarkLikes(remarkList);
		model.addAttribute("release", release);
		model.addAttribute("user", user);
		model.addAttribute("menu", menurService.list());
		model.addAttribute("tuijian", courseService.listXie());
		model.addAttribute("remark", releaseRemarkLikes);
		return "default/detail2.html";
	}

	@RequestMapping("/detail3/{id}/")
	public String detail3(@PathVariable("id") Integer id,Model model) {
		Subject subject = SecurityUtils.getSubject();
		Object object = subject.getPrincipal();
		if(object != null) {
			model.addAttribute("user", (UserTwo)object);
		}
		TArticle article = itArticleService.getById(id);
		UserTwo user = usertwoService.getById(article.getUserId());
		TArticleType type = itArticleTypeService.getById(article.getType());
		//先拿到所有评论
		List<TArticleRemark> remarkList =  itArticleRemarkService.getAllArticleRemark(id);
		//查询每一条评论的点赞量
//		for (TArticleRemark remark:remarkList) {
//			Integer number = itLikesService.getRemarkLikesNumber(remark.getId(), 1);
//			remark.setLikesNumber(number);
//		}
		List<TArticleRemark> remarkLikes = getArticleRemarkLikes(remarkList);
		model.addAttribute("article", article);
		model.addAttribute("type", type);
		model.addAttribute("user", user);
		model.addAttribute("menu", menurService.list());
		model.addAttribute("tuijian", itArticleService.listXie());
		model.addAttribute("remark", remarkLikes);
		return "default/detail3.html";
	}

	@RequestMapping("/detail4/{id}/")
	public String detail4(@PathVariable("id") Integer id,Model model) {
		Subject subject = SecurityUtils.getSubject();
		Object object = subject.getPrincipal();
		if(object != null) {
			model.addAttribute("user", (UserTwo)object);
		}

		Teacher teacher = teacherService.getById(id);
		model.addAttribute("teacher", teacher);
		model.addAttribute("menu", menurService.list());
		return "default/detail4.html";
	}

	/**
	 * 文章评论
	 * 遍历所有评论，查询评论的点赞量
	 * @return
	 */
	public List<TArticleRemark> getArticleRemarkLikes(List<TArticleRemark> remarkList){
		for (TArticleRemark remark:remarkList) {
			Integer number = itLikesService.getRemarkLikesNumber(remark.getId(), 1);
			remark.setLikesNumber(number);
			if (remark.getChildrenRemarkList().size() > 0) {
				getArticleRemarkLikes(remark.getChildrenRemarkList());
			}
		}
		return  remarkList;
	}

	public List<TCourseRemark> getCourseRemarkLikes(List<TCourseRemark> remarkList){
		for (TCourseRemark remark:remarkList) {
			Integer number = itLikesService.getRemarkLikesNumber(remark.getId(), 2);
			remark.setLikesNumber(number);
			if (remark.getChildrenRemarkList().size() > 0) {
				getCourseRemarkLikes(remark.getChildrenRemarkList());
			}
		}
		return  remarkList;
	}

	public List<TReleaseRemark> getReleaseRemarkLikes(List<TReleaseRemark> remarkList){
		for (TReleaseRemark remark:remarkList) {
			Integer number = itLikesService.getRemarkLikesNumber(remark.getId(), 3);
			remark.setLikesNumber(number);
			if (remark.getChildrenRemarkList().size() > 0) {
				getReleaseRemarkLikes(remark.getChildrenRemarkList());
			}
		}
		return  remarkList;
	}


	@RequestMapping("/pay/{id}/")
	public String pay(@PathVariable("id") Integer id,Model model) {
		Subject subject = SecurityUtils.getSubject();
        Object object = subject.getPrincipal();
        if(object != null) {
        	model.addAttribute("user", (UserTwo)object);
        }
		Course course = courseService.getById(id);
		model.addAttribute("course", course);
		model.addAttribute("menu", menurService.list());
		return "default/pay.html";
	}
	
	@RequestMapping("/release")
	public String release(Model model) {
		Subject subject = SecurityUtils.getSubject();
        Object object = subject.getPrincipal();
        if(object != null) {
        	model.addAttribute("user", (UserTwo)object);
        }
		model.addAttribute("menu", menurService.list());
		return "default/release.html";
	}

	/**
	 * 跳转发布文章界面
	 * @param model
	 * @return
	 */
	@RequestMapping("/article-release")
	public String articleRelease(Model model) {
		Subject subject = SecurityUtils.getSubject();
		Object object = subject.getPrincipal();
		if(object != null) {
			model.addAttribute("user", (UserTwo)object);
		}
		model.addAttribute("menu", menurService.list());
		model.addAttribute("typeList", itArticleTypeService.list());
		return "default/article-release.html";
	}

	@RequestMapping("/allArticle")
	public String allArticle(Model model){
		Subject subject = SecurityUtils.getSubject();
		Object object = subject.getPrincipal();

		if(object != null) {
			model.addAttribute("user", (UserTwo)object);
		}
		model.addAttribute("menu", menurService.list());
		model.addAttribute("article", itArticleService.list());
		model.addAttribute("type", itArticleTypeService.list());
		return "default/articleList.html";

	}

	/**
	 * 文章分页查询
	 */
	@ResponseBody
	@RequestMapping("/article/page")
	public PageResult<TArticle> articlePage(HttpServletRequest request) {
		QueryWrapper<TArticle> wrapper = new QueryWrapper<>();
		wrapper.eq("state",1);
		PageParam<TArticle> pageParam = new PageParam<>(request);
		return new PageResult<>(itArticleService.page(pageParam, pageParam.getOrderWrapper(wrapper)).getRecords(), pageParam.getTotal());
		//return classifyService.listPage(pageParam);  // 使用关联查询
	}

	/**
	 * 按类型分页查询
	 */
	@ResponseBody
	@RequestMapping("/article/pageByType")
	public PageResult<TArticle> pageByType(HttpServletRequest request) {
		String type = request.getParameter("type");
		log.info("type: {}",type);
		QueryWrapper<TArticle> wrapper = new QueryWrapper<>();
		wrapper
				.eq("type",type)
				.eq("state",1);
		PageParam<TArticle> pageParam = new PageParam<>();

		return new PageResult<>(itArticleService.page(pageParam, pageParam.getOrderWrapper(wrapper)).getRecords(), pageParam.getTotal());
		//return classifyService.listPage(pageParam);  // 使用关联查询
	}

	/**
	 * 搜索文章
	 */
	@ResponseBody
	@RequestMapping("/article/search")
	public PageResult<TArticle> searchArticle(HttpServletRequest request) {
		String name = request.getParameter("name");
		log.info("type: {}",name);
		QueryWrapper<TArticle> wrapper = new QueryWrapper<>();
		wrapper
				.like("name",name)
				.eq("state",1);
		PageParam<TArticle> pageParam = new PageParam<>();

		return new PageResult<>(itArticleService.page(pageParam, pageParam.getOrderWrapper(wrapper)).getRecords(), pageParam.getTotal());
		//return classifyService.listPage(pageParam);  // 使用关联查询
	}


	@ResponseBody
	@RequestMapping("/article/save")
	public JsonResult save(ArticleDto articleDto) {
		TArticle article = new TArticle();
		BeanUtils.copyProperties(articleDto,article);

		//TODO:4.部署阿里云打开
		UrlTranslate urlTranslate = new UrlTranslate();
		String url = urlTranslate.imageOrVideoUrlTranslate("文章", articleDto.getImage());
		article.setImage(url);

		Subject subject = SecurityUtils.getSubject();
		Object object = subject.getPrincipal();
		Integer userId;
		if (object != null) {
			try {
				userId = ((UserTwo) object).getUserId();
			}catch (ClassCastException e){
				userId = 0;

			}
			article.setUserId(userId);
			article.setDelFlag(0);
			article.setCreateTime(new Date());
			article.setState(0);
			if (itArticleService.save(article)) {
				return JsonResult.ok("添加成功");
			}else {
				return JsonResult.error("添加失败");
			}

		}else{
			return JsonResult.error("请先登录");
		}

	}

	/**
	 * 用户发布界面
	 * @param model
	 * @return
	 */
	@RequestMapping("/releases")
	public String releases(Model model) {
		Subject subject = SecurityUtils.getSubject();
        Object object = subject.getPrincipal();
        if(object != null) {
        	model.addAttribute("user", (UserTwo)object);
        }
		model.addAttribute("menu", menurService.list());
		model.addAttribute("lunbo", lunboService.list());
		return "default/releases.html";
	}


	
	/**
     * 分页查询
     */
    @ResponseBody
    @RequestMapping("/course/page")
    public PageResult<Course> page(HttpServletRequest request) {
        PageParam<Course> pageParam = new PageParam<>(request);
        return new PageResult<>(courseService.page(pageParam, pageParam.getWrapper()).getRecords(), pageParam.getTotal());
    }
    
    /**
     * 分页查询
     */
    @ResponseBody
    @RequestMapping("/present/page")
    public PageResult<Present> pages(HttpServletRequest request) {
        PageParam<Present> pageParam = new PageParam<>(request);
        return new PageResult<>(presentService.page(pageParam, pageParam.getWrapper()).getRecords(), pageParam.getTotal());
    }
    
    /**
     * 分页查询
     */
    @ResponseBody
    @RequestMapping("/release/page")
    public PageResult<Release> pagess(HttpServletRequest request) {
        PageParam<Release> pageParam = new PageParam<>(request);
        return new PageResult<>(releaseService.page(pageParam, pageParam.getWrapper()).getRecords(), pageParam.getTotal());
    }
    
    @PostMapping("/pay")
    @ResponseBody
    public JsonResult pays(String type,String name,String jine) {
    	Subject subject = SecurityUtils.getSubject();
        Object object = subject.getPrincipal();
        if (object != null) {
        	String orderNo = "COURSE" + (int)((Math.random()*9+1)*100000);
        	Order order = new Order();
        	order.setCreateTime(new Date());
        	order.setJine(jine);
        	order.setName(name);
        	order.setOrderNo(orderNo);
        	order.setType(type);
        	order.setUserId(((UserTwo)object).getUserId());
        	Boolean res = orderService.save(order);
        	if(res) {
        		return JsonResult.ok("支付成功");
        	}else {
        		return JsonResult.error("支付失败");
        	}
        } else {
        	return JsonResult.error("请先登录");
        }
    }
    
    @PostMapping("/releaseSave")
    @ResponseBody
    public JsonResult releaseSave(String name,String content,String url,String image) {
    	Subject subject = SecurityUtils.getSubject();
        Object object = subject.getPrincipal();

		//TODO:3.部署阿里云打开
		UrlTranslate urlTranslate = new UrlTranslate();
		url = urlTranslate.imageOrVideoUrlTranslate("课程",url);


        if (object != null) {
        	Release releae = new Release();
        	releae.setCreateTime(new Date());
        	releae.setContent(content);
        	releae.setName(name);
        	releae.setState(0);
        	releae.setUrl(url);
        	releae.setImage(image);
        	releae.setUserId(((UserTwo)object).getUserId());
        	Boolean res = releaseService.save(releae);
        	if(res) {
        		return JsonResult.ok("保存成功");
        	}else {
        		return JsonResult.error("保存失败");
        	}
        } else {
        	return JsonResult.error("请先登录");
        }
    }

	/**
	 * 师资界面
	 * @param model
	 * @return
	 */
	@RequestMapping("/teacher")
	public String teacher(Model model){
		Subject subject = SecurityUtils.getSubject();
		Object object = subject.getPrincipal();

		if(object != null) {
			model.addAttribute("user", (UserTwo)object);
		}
		model.addAttribute("menu", menurService.list());
		model.addAttribute("teacher", teacherService.list());
		return "default/teacher.html";

	}

	/**
	 * 教师分页查询
	 */
	@ResponseBody
	@RequestMapping("/teacher/page")
	public PageResult<Teacher> teacherPage(HttpServletRequest request) {
		PageParam<Teacher> pageParam = new PageParam<>(request);
		return new PageResult<>(teacherService.page(pageParam, pageParam.getWrapper()).getRecords(), pageParam.getTotal());
		//return classifyService.listPage(pageParam);  // 使用关联查询
	}

	/**
	 * 保存评论
	 * @param dto
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/remark/saveRemark")
	public JsonResult saveRemark(CourseRemarkDto dto) {
		TCourseRemark remark = new TCourseRemark();
		Subject subject = SecurityUtils.getSubject();
		Object object = subject.getPrincipal();
		Integer userId;
		if (object != null) {
			try {
				userId = ((UserTwo) object).getUserId();
			}catch (ClassCastException e){
				userId = 0;
			}
			BeanUtils.copyProperties(dto,remark);
			remark.setUserId(userId);
			remark.setState(0);
			remark.setCreateTime(new Date());
			remark.setName(dto.getContent());
			if (itCourseRemarkService.save(remark)) {
				return JsonResult.ok("添加成功");
			}else {
				return JsonResult.error("添加失败");
			}

		}else{
			return JsonResult.error("请先登录");
		}

	}

	/**
	 * 添加课程评论
	 */
	@ResponseBody
	@RequestMapping("/saveCourseRemark")
	public JsonResult saveCourseRemark(Integer id,String content) {
		log.info("看这里------->: {}",id);
		log.info("看这里------->: {}",content);

		TCourseRemark remark = new TCourseRemark();
		Subject subject = SecurityUtils.getSubject();
		Object object = subject.getPrincipal();
		Integer userId;
		if (object != null) {
			try {
				userId = ((UserTwo) object).getUserId();
			}catch (ClassCastException e){
				userId = 0;

			}
			remark.setContent(content);
			remark.setName(content);
			remark.setUserId(userId);
			remark.setCreateTime(new Date());
			remark.setParentId(0);
			remark.setState(0);
			remark.setCourseId(id);
			if (itCourseRemarkService.save(remark)) {
				return JsonResult.ok("添加成功");
			}else {
				return JsonResult.error("添加失败");
			}
		}else {
			return JsonResult.error("请先登录");
		}

	}


	/**
	 * 回复课程评论
	 */
	@ResponseBody
	@RequestMapping("/replyCourseRemark")
	public JsonResult replyCourseRemark(Integer parentId,String content) {
		log.info("看这里------->: {}",parentId);
		log.info("看这里------->: {}",content);
		TCourseRemark parentRemark = itCourseRemarkService.getById(parentId);
		Integer courseId = parentRemark.getCourseId();

		TCourseRemark remark = new TCourseRemark();
		Subject subject = SecurityUtils.getSubject();
		Object object = subject.getPrincipal();
		Integer userId;
		if (object != null) {
			try {
				userId = ((UserTwo) object).getUserId();
			}catch (ClassCastException e){
				userId = 0;

			}
			remark.setContent(content);
			remark.setName(content);
			remark.setUserId(userId);
			remark.setCreateTime(new Date());
			remark.setParentId(0);
			remark.setState(0);
			remark.setCourseId(courseId);
			remark.setParentId(parentId);
			if (itCourseRemarkService.save(remark)) {
				return JsonResult.ok("添加成功");
			}else {
				return JsonResult.error("添加失败");
			}
		}else {
			return JsonResult.error("请先登录");
		}

	}

	/**
	 * 添加发布评论
	 */
	@ResponseBody
	@RequestMapping("/SaveReleaseRemark")
	public JsonResult SaveReleaseRemark(Integer id,String content) {
		log.info("看这里------->: {}",id);
		log.info("看这里------->: {}",content);

		TReleaseRemark remark = new TReleaseRemark();
		Subject subject = SecurityUtils.getSubject();
		Object object = subject.getPrincipal();
		Integer userId;
		if (object != null) {
			try {
				userId = ((UserTwo) object).getUserId();
			}catch (ClassCastException e){
				userId = 0;

			}
			remark.setContent(content);
			remark.setName(content);
			remark.setUserId(userId);
			remark.setCreateTime(new Date());
			remark.setParentId(0);
			remark.setStart(0);
			remark.setReleaseId(id);
			if (itReleaseRemarkService.save(remark)) {
				return JsonResult.ok("添加成功");
			}else {
				return JsonResult.error("添加失败");
			}
		}else {
			return JsonResult.error("请先登录");
		}

	}

	/**
	 * 回复发布评论
	 */
	@ResponseBody
	@RequestMapping("/replyReleaseRemark")
	public JsonResult replyReleaseRemark(Integer parentId,String content) {
		log.info("看这里------->: {}",parentId);
		log.info("看这里------->: {}",content);
		TReleaseRemark parentRemark = itReleaseRemarkService.getById(parentId);
		Integer releaseId = parentRemark.getReleaseId();

		TReleaseRemark remark = new TReleaseRemark();
		Subject subject = SecurityUtils.getSubject();
		Object object = subject.getPrincipal();
		Integer userId;
		if (object != null) {
			try {
				userId = ((UserTwo) object).getUserId();
			}catch (ClassCastException e){
				userId = 0;

			}
			remark.setContent(content);
			remark.setName(content);
			remark.setUserId(userId);
			remark.setCreateTime(new Date());
			remark.setParentId(0);
			remark.setStart(0);
			remark.setReleaseId(releaseId);
			remark.setParentId(parentId);
			if (itReleaseRemarkService.save(remark)) {
				return JsonResult.ok("添加成功");
			}else {
				return JsonResult.error("添加失败");
			}
		}else {
			return JsonResult.error("请先登录");
		}

	}

	/**
	 * 添加文章评论
	 */
	@ResponseBody
	@RequestMapping("/SaveArticleRemark")
	public JsonResult SaveArticleRemark(Integer id,String content) {
		log.info("看这里------->: {}",id);
		log.info("看这里------->: {}",content);

		TArticleRemark remark = new TArticleRemark();
		Subject subject = SecurityUtils.getSubject();
		Object object = subject.getPrincipal();
		Integer userId;
		if (object != null) {
			try {
				userId = ((UserTwo) object).getUserId();
			}catch (ClassCastException e){
				userId = 0;

			}
			remark.setContent(content);
			remark.setName(content);
			remark.setUserId(userId);
			remark.setCreateTime(new Date());
			remark.setParentId(0);
			remark.setState(0);
			remark.setArticlerId(id);
			if (itArticleRemarkService.save(remark)) {
				return JsonResult.ok("添加成功");
			}else {
				return JsonResult.error("添加失败");
			}
		}else {
			return JsonResult.error("请先登录");
		}

	}

	/**
	 * 回复发布评论
	 */
	@ResponseBody
	@RequestMapping("/replyArticleRemark")
	public JsonResult replyArticleRemark(Integer parentId,String content) {
		log.info("看这里------->: {}",parentId);
		log.info("看这里------->: {}",content);
		TArticleRemark parentRemark = itArticleRemarkService.getById(parentId);
		Integer articlerId = parentRemark.getArticlerId();

		TArticleRemark remark = new TArticleRemark();
		Subject subject = SecurityUtils.getSubject();
		Object object = subject.getPrincipal();
		Integer userId;
		if (object != null) {
			try {
				userId = ((UserTwo) object).getUserId();
			}catch (ClassCastException e){
				userId = 0;

			}
			remark.setContent(content);
			remark.setName(content);
			remark.setUserId(userId);
			remark.setCreateTime(new Date());
			remark.setParentId(0);
			remark.setState(0);
			remark.setArticlerId(articlerId);
			remark.setParentId(parentId);
			if (itArticleRemarkService.save(remark)) {
				return JsonResult.ok("添加成功");
			}else {
				return JsonResult.error("添加失败");
			}
		}else {
			return JsonResult.error("请先登录");
		}

	}

	//TODO:添加点赞功能、视频播放量功能

    /**
     * 评论点赞
     */
    @ResponseBody
    @RequestMapping("/addArticleRemarkLikes")
    public JsonResult addArticleRemarkLikes(Integer id,Integer type) {
        log.info("看这里------->: {}",id);

		TArticleRemark remark = itArticleRemarkService.getById(id);
		String content = remark.getContent();
		log.info("看这里------->: {}",content);

		Subject subject = SecurityUtils.getSubject();
        Object object = subject.getPrincipal();
        Integer userId;
        if (object != null) {
            try {
                userId = ((UserTwo) object).getUserId();
            }catch (ClassCastException e){
                userId = 0;

            }

			QueryWrapper<TLikes> wrapper = new QueryWrapper<>();
			wrapper
					.eq("remark_id",id)
					.eq("user_id",userId)
					.eq("type",type);
			TLikes tLikes = tLikesMapper.selectOne(wrapper);
			if (tLikes != null) {
				itLikesService.removeById(tLikes.getId());
				return JsonResult.ok(3,"成功!");
			}else {
				tLikes = new TLikes();
				tLikes.setName(content);
				tLikes.setUserId(userId);
				tLikes.setRemarkId(id);
				tLikes.setType(type);
				itLikesService.save(tLikes);
				return JsonResult.ok(0,"成功!");
			}
        }else {
			return JsonResult.error("请先登录");
        }

    }

	/**
	 * 评论点赞
	 */
	@ResponseBody
	@RequestMapping("/addReleaseRemarkLikes")
	public JsonResult addReleaseRemarkLikes(Integer id,Integer type) {
		log.info("看这里------->: {}",id);

		TReleaseRemark remark = itReleaseRemarkService.getById(id);
		String content = remark.getContent();
		log.info("看这里------->: {}",content);

		Subject subject = SecurityUtils.getSubject();
		Object object = subject.getPrincipal();
		Integer userId;
		if (object != null) {
			try {
				userId = ((UserTwo) object).getUserId();
			}catch (ClassCastException e){
				userId = 0;
			}

			QueryWrapper<TLikes> wrapper = new QueryWrapper<>();
			wrapper
					.eq("remark_id",id)
					.eq("user_id",userId)
					.eq("type",type);
			TLikes tLikes = tLikesMapper.selectOne(wrapper);
			if (tLikes != null) {
				itLikesService.removeById(tLikes.getId());
				return JsonResult.ok(3,"成功!");
			}else {
				tLikes = new TLikes();
				tLikes.setName(content);
				tLikes.setUserId(userId);
				tLikes.setRemarkId(id);
				tLikes.setType(type);
				itLikesService.save(tLikes);
				return JsonResult.ok(0,"成功!");
			}
		}else {
			return JsonResult.error("请先登录");
		}
	}

	@ResponseBody
	@RequestMapping("/addCourseRemarkLikes")
	public JsonResult addCourseRemarkLikes(Integer id,Integer type) {
		log.info("看这里------->: {}",id);

		TCourseRemark remark = itCourseRemarkService.getById(id);
		String content = remark.getContent();
		log.info("看这里------->: {}",content);

		Subject subject = SecurityUtils.getSubject();
		Object object = subject.getPrincipal();
		Integer userId;
		if (object != null) {
			try {
				userId = ((UserTwo) object).getUserId();
			}catch (ClassCastException e){
				userId = 0;
			}

			QueryWrapper<TLikes> wrapper = new QueryWrapper<>();
			wrapper
					.eq("remark_id",id)
					.eq("user_id",userId)
					.eq("type",type);
			TLikes tLikes = tLikesMapper.selectOne(wrapper);
			if (tLikes != null) {
				itLikesService.removeById(tLikes.getId());
				return JsonResult.ok(3,"成功!");
			}else {
				tLikes = new TLikes();
				tLikes.setName(content);
				tLikes.setUserId(userId);
				tLikes.setRemarkId(id);
				tLikes.setType(type);
				itLikesService.save(tLikes);
				return JsonResult.ok(0,"成功!");
			}
		}else {
			return JsonResult.error("请先登录");
		}
	}


    /**
     * 登录
     */
	@RequestMapping("/login")
    public String login(Model model) {
		
//		 Subject subject = SecurityUtils.getSubject();
//        Object object = subject.getPrincipal();
//        if (object != null) {
//        	model.addAttribute("menu", menurService.list());
//        	model.addAttribute("user", (UserTwo)object);
//        	return "redirect:/users/index";
//        }else {

        	UserTwo user2 = new UserTwo();
			user2.setUsername("游客");
			user2.setUserId(-1);
			model.addAttribute("user", user2);
			model.addAttribute("menu", menurService.list());
			return "default/login.html";

		//}
		
        
    }

	/**
     * 用户登录
     */
    @ResponseBody
    @PostMapping("/tologin")
    public JsonResult tologin(String username, String password, String code, Boolean remember, HttpServletRequest request) {
        if (username == null || username.trim().isEmpty()) return JsonResult.error("请输入账号");
        if (!CaptchaUtil.ver(code, request)) {
            return JsonResult.error("验证码不正确");
        }

        try {
            if (remember == null) remember = false;
            SecurityUtils.getSubject().login(new CustomizedToken(username, password, remember,"User"));
            return JsonResult.ok("登录成功").put("action", "/users/index").put("status", 0);
        } catch (IncorrectCredentialsException ice) {
            return JsonResult.error("密码错误");
        } catch (UnknownAccountException uae) {
            return JsonResult.error("账号不存在");
        } catch (LockedAccountException e) {
            return JsonResult.error("账号被锁定");
        } catch (ExcessiveAttemptsException eae) {
            return JsonResult.error("操作频繁，请稍后再试");
        }
    }
    
    @RequestMapping(value = "/logout")
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/default/login";
    }
}
